package com.crm.controller.system;

import com.crm.dto.MenuDto;
import com.crm.pojo.Menu;
import com.crm.result.ResponseResult;
import com.crm.result.Result;
import com.crm.service.system.MenuService;
import com.crm.vo.TreeselectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/system/menu")
@Api(tags = "菜单管理相关接口")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    @ApiOperation("查询菜单列表")
    public Result queryMenuList() {
        log.info("查询菜单列表");
        List<Menu> result = menuService.queryMenuList();
        return Result.success(result);
    }

    /**
     * 新增菜单
     */
    @PostMapping
    @ApiOperation("新增菜单")
    public ResponseResult addMenu(@RequestBody MenuDto dto) {
        log.info("新增菜单参数{}", dto);
        menuService.addMenu(dto);
        return ResponseResult.success();
    }

    /**
     * 根据菜单id查询菜单信息
     */
    @GetMapping("/{menuId}")
    public Result queryMenuById(@PathVariable Long menuId) {
        log.info("根据id查询菜单{}", menuId);
        Menu result = menuService.queryById(menuId);
        return Result.success(result);
    }

    /**
     * 修改菜单
     */
    @PutMapping
    public ResponseResult updateMenu(@RequestBody Menu menu) {
        log.info("修改菜单数据{}", menu);
        menuService.updateMenu(menu);
        return ResponseResult.success();
    }


    /**
     * 根据菜单id删除菜单
     */
    @DeleteMapping("/{menuId}")
    @ApiOperation("删除菜单")
    public ResponseResult deleteMenu(@PathVariable Long menuId) {
        log.info("根据id{}删除菜单", menuId);
        menuService.deleteMenu(menuId);
        return ResponseResult.success();
    }

    @GetMapping("/treeselect")
    public ResponseResult getTree() {
        List<TreeselectVO> menuList = menuService.getTree();
        return ResponseResult.success(menuList);
    }


}
